package com.ysm.smfu.holder;

import android.app.Activity;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestManager;
import com.ysm.smfu.R;
import com.ysm.smfu.activity.CategoryActivity;
import com.ysm.smfu.activity.SearchActivity;
import com.ysm.smfu.bean.HomeSearchResultBean;
import com.ysm.smfu.fragment.BendiFragment;
import com.ysm.smfu.fragment.HomeFragment;
import com.ysm.smfu.utils.PrintUtil;
import com.ysm.smfu.utils.UIUtils;
import com.ysm.smfu.view.RecyclerImageView;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/9/21.
 */
public class BaseListHolder extends BaseHolder<HomeSearchResultBean.BusinessInfo> {

    private Object object;
    @BindView(R.id.home_list_item_img)
    RecyclerImageView homeListItemImg;
    @BindView(R.id.home_list_item_title)
    TextView homeListItemTitle;
    @BindView(R.id.home_list_item_profile)
    TextView homeListItemProfile;
    @BindView(R.id.home_list_item_location)
    TextView homeListItemLocation;
    @BindView(R.id.home_list_item_distance)
    TextView homeListItemDistance;
    private View view;

    public BaseListHolder(Object object) {
        this.object = object;
    }

    @Override
    protected View initView() {
        view = UIUtils.inflate(R.layout.home_listview_item);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    protected void refreshView(HomeSearchResultBean.BusinessInfo data) {
        PrintUtil.printRespones("商户头像地址:" + data.headPhoto);
        if (!TextUtils.isEmpty(data.headPhoto) && !data.headPhoto.startsWith("null?")) {
            translateObjectAndLoadImg(data);
        }
        homeListItemTitle.setText(data.name);
//        if (!TextUtils.isEmpty(data.discount))
        homeListItemProfile.setText("进店送" + data.pointsRatio + "%兑换券");
        homeListItemLocation.setText(data.className + "/" + data.cityName + data.countryName);
        if (!TextUtils.equals(data.distance, "0")) {
            homeListItemDistance.setText("距离" + data.distance);
            homeListItemDistance.setVisibility(View.VISIBLE);
        } else
            homeListItemDistance.setVisibility(View.GONE);
    }

    /**
     * 转换object对象,并加载图片
     *
     * @param data
     */
    private void translateObjectAndLoadImg(HomeSearchResultBean.BusinessInfo data) {
        if (object instanceof CategoryActivity)
            loadImageWithUrl(data, (CategoryActivity) object, null);
        else if (object instanceof SearchActivity)
            loadImageWithUrl(data, (SearchActivity) object, null);
        else if (object instanceof BendiFragment)
            loadImageWithUrl(data, null, (BendiFragment) object);
        else if (object instanceof HomeFragment)
            loadImageWithUrl(data, null, (HomeFragment) object);
    }

    /**
     * 从网络加载图片
     */
    private void loadImageWithUrl(HomeSearchResultBean.BusinessInfo data, Activity activity, Fragment fragment) {
        RequestManager manager = null;
        if (activity != null) {
            manager = Glide.with(activity);
        } else if (fragment != null)
            manager = Glide.with(fragment);
        manager.load(data.headPhoto)
                .placeholder(R.drawable.pay_businessicon_defualt)
                .error(R.drawable.pay_businessicon_defualt)
                .override(140, 140)
//            显示缩略图
                .thumbnail(0.1f)
                .centerCrop()
                .into(homeListItemImg);
    }
}
